package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Override
    public PageResponse<NursingPlanVo> paramQuery(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> planPage = nursingPlanMapper.Pageselect(name, status);
        return PageResponse.of(planPage, NursingPlanVo.class);
    }

    @Override
    @Transactional
    public void addNursingPlan(NursingPlanDto dto) {
        //dto转entity
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlan.setCreateTime(LocalDateTime.now());
        //校验添加的护理计划是否已存在
        String planName = nursingPlan.getPlanName();
        Integer count = nursingPlanMapper.selectCountByPlanName(planName);
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSING_PLAN_NO_EXIST);
        }
        //调用mapper
        log.info("在这里看qian,{}", nursingPlan.getId());
        nursingPlanMapper.insert(nursingPlan);
        log.info("在这里看hou,{}", nursingPlan.getId());
        if (!CollectionUtils.isEmpty(nursingPlan.getProjectPlans())) {

            nursingPlan.getProjectPlans().forEach(e -> {

                //将护理计划id(planId)写入每一个护理项目中
                e.setPlanId(nursingPlan.getId());

            });
            List<NursingProjectPlanVo> projectPlans = nursingPlan.getProjectPlans();
            //将vo转entity
            List<NursingProjectPlan> list = BeanUtil.copyToList(projectPlans, NursingProjectPlan.class);
            nursingPlanMapper.insertNursingPlan(list);

        }
    }

    @Override
    public List<NursingPlanVo> findAllNursingPlan() {
        List<NursingPlanVo> voList = nursingPlanMapper.selectAllNursingPlan();
        return voList;
    }

    @Override
    public NursingPlanVo selectById(Integer id) {
        //调用Mapper
        NursingPlanVo vo = nursingPlanMapper.selectById(id);


        return vo;
    }

    @Override
    public void modifyNursingPlan(Long id, NursingPlanDto dto) {

        //根据护理计划id将nursing_project_plan删除
        nursingPlanMapper.deleteNursingProjectPlanById(id);
        //将dto转entity
        NursingPlan entity = BeanUtil.toBean(dto, NursingPlan.class);
        //判断修改的护理计划的名称是否已经存在
        Integer count = nursingPlanMapper.selectModifyCountByPlanName(entity.getPlanName(),entity.getId());
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSING_PLAN_NO_EXIST);
        }
        //将entity中的数据写入nursing_project_plan
        if (!CollectionUtils.isEmpty(entity.getProjectPlans())) {
            entity.getProjectPlans().forEach(e -> {
                e.setPlanId(id);
            });
            List<NursingProjectPlanVo> projectPlans = entity.getProjectPlans();
            //将vo转entity
            List<NursingProjectPlan> list = BeanUtil.copyToList(projectPlans, NursingProjectPlan.class);
            //planId projectId 插入nursing_project_plan中
            nursingPlanMapper.insertNursingPlan(list);
        }
        //修改nursing_plan中的信息
        nursingPlanMapper.update(entity);

    }

    @Override
    public void deleteNursingPlan(Long id) {
        nursingPlanMapper.deleteNursingPlanById(id);
        nursingPlanMapper.deleteNursingProjectPlanById(id);
    }

    @Override
    public void nursingPlanStatus(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id, status);
    }
}
